﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using myMvcBlog.Infrastructure;

namespace myMvcBlog.Controllers
{
    public class RSSController : Controller
    {
        //
        // GET: /RSS/

        public RssResult Index()
        {

            SyndicationFeed feed = new SyndicationFeed("Jun1st's Notes",
                                                       "RSS Feed of Jun1st's Notes",
                                                       new Uri("http://fengqijun.com"),
                                                       "Jun1st",
                                                       DateTime.Now);                                                       
            IList<SyndicationItem> sis = new List<SyndicationItem>();
            var posts = Post.All().OrderByDescending( p => p.PostedTime ).ToList();

            foreach (var item in posts)
            {
                SyndicationItem si = new SyndicationItem(item.Name,
                                                         item.Content,
                                                         new Uri("http://fengqijun.com/posts/" + item.UrlName),
                                                         item.UrlName,
                                                         item.PostedTime);

                sis.Add(si);
            }

            feed.Items = sis;

            return new RssResult() { Feed = feed };
        }

    }
}
